defmodule Giom.Accounting.Ptrans do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:doc_no, :string, autogenerate: false}
  @derive {Phoenix.Param, key: :doc_no}
  schema "ptrans" do
    field :date, :utc_datetime
    field :source_code, :string
    field :target_code, :string
    field :doc_type,  :string, size: 1
    field :comment,  :string
    
    field :amount, :decimal
    field :vat_total, :decimal
    field :amount_vat, :decimal
    field :vat_status, :decimal
    field :vat1sum, :decimal
    field :vat8sum, :decimal
    field :vat18sum, :decimal
    field :qty_row, :integer
    field :qty_total, :decimal
    #field :create_user, :string
    #field :edit_user, :string
    field :create_date, :utc_datetime
    field :edit_date, :utc_datetime
    field :expiry_date, :utc_datetime
    field :oper_type,  :string, size: 1
    field :wbill_code,  :string
    field :integration,  :string, size: 1
    

    timestamps()
  end

  @doc false
  def changeset(ptrans, attrs) do
    ptrans
    |> cast(attrs, [:date])
    |> validate_required([:date])
    |> unique_constraint(:doc_no, name: :ptrans_pkey)
    |> foreign_key_constraint(:source_code)
    |> foreign_key_constraint(:target_code)
  end
end
